using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.Tilemaps;

public class LevelController : MonoBehaviour
{
    public Tilemap groundTileMap;
    public Tilemap floorTileMap;

    public TileBase wall0;
    public TileBase wall1;
    public TileBase wall2;
    public TileBase wall3;

    public TileBase floor0;
    public TileBase floor1;
    public TileBase floor2;

    private TileBase floor
    {
        get
        {
            var index = Random.Range(0, 3);
            switch (index)
            {
                case 0:
                    return floor0;
                case 1:
                    return floor1;
                case 2:
                    return floor2;
                default:
                    return floor0;
            }
        }
    }
    private TileBase ground
    {
        get
        {
            var index = Random.Range(0, 4);
            switch (index)
            {
                case 0:
                    return wall0;
                case 1:
                    return wall1;
                case 2:
                    return wall2;
                case 3:
                    return wall3;
                default:
                    return wall0;
            }
        }
    }
    public GameObject final;
    // Start is called before the first frame update
    public GameObject player;
    public GameObject enemy;

    private int startX = 0;

    /*
    * 1为地块
    * @为玩家
    * e为敌人
    * #为终点
    */
    public List<string> initRoom { get; set; } = new List<string>() {
        "1111111111",
        "1        1",
        "1        1",
        "1        1",
        "1    @    ",
        "1         ",
        "1        1",
        "1        1",
        "1        1",
        "1111111111"
    };

    public List<string> normalRoom { get; set; } = new List<string>() {
        "1111111111",
        "1   e    1",
        "1        1",
        "1        1",
        "          ",
        "     e    ",
        "1        1",
        "1        1",
        "1      e 1",
        "1111111111"
    };

    public List<string> finalRoom { get; set; } = new List<string>() {
        "1111111111",
        "1        1",
        "1        1",
        "1        1",
        "     #   1",
        "         1",
        "1        1",
        "1        1",
        "1        1",
        "1111111111"
    };

    void Start()
    {
        GenerateRoom(initRoom, startX);
        startX += initRoom.First().Length + 2;
        GenerateRoom(normalRoom, startX);
        startX += normalRoom.First().Length + 2;
        GenerateRoom(finalRoom, startX);
        startX += normalRoom.First().Length + 2;
    }

    void GenerateRoom(List<string> room, int startX)
    {
        for (int i = 0; i < room.Count; i++)
        {
            string line = room[i];
            for (int j = 0; j < line.Length; j++)
            {
                int x = startX + j;
                int y = room.Count - i;
                floorTileMap.SetTile(new Vector3Int(x, y, 0), floor);
                switch (line[j])
                {
                    case '1':
                        {
                            groundTileMap.SetTile(new Vector3Int(x, y, 0), ground);
                        }
                        break;
                    case '@':
                        {
                            GameObject go = Instantiate(player, new Vector3(x + 0.5f, y + 0.5f, 0), Quaternion.identity);
                            go.SetActive(true);
                            Global.player = go.GetComponent<Player>();
                        }
                        break;
                    case 'e':
                        {
                            GameObject go = Instantiate(enemy, new Vector3(x + 0.5f, y + 0.5f, 0), Quaternion.identity);
                            go.SetActive(true);
                        }
                        break;
                    case '#':
                        {
                            GameObject go = Instantiate(final, new Vector3(x + 0.5f, y + 0.5f, 0), Quaternion.identity);
                            go.SetActive(true);
                        }
                        break;
                }
            }
        }
    }
}
